package utils;

import java.util.*;

/**
 * @Package: LOL位置分发器
 * @Description TODO
 * @Author xiWei
 * @WeChat XW-MXN
 * @Date 2025/2/18 17:24
 * @Version V2.0.0
 */
public class LOLwzffq {
    // 核心分配逻辑
    public static Map<String, String> assignPositions(List<String> players, Map<String, List<String>> preferences,String luren) {
        List<String> allPositions = new ArrayList<>(Arrays.asList("上单", "中单", "打野", "射手", "辅助"));
        //如果存在路人，则路人先选择位置
        allPositions.remove(luren);
        Map<String, String> assignments = new LinkedHashMap<>();
        List<String> remainingPositions = new ArrayList<>(allPositions);

        // 第一阶段：优先分配偏好位置
        for (String player : players) {
            List<String> preferred = new ArrayList<>(preferences.getOrDefault(player, Collections.emptyList()));
            preferred.retainAll(remainingPositions); // 仅保留可用位置

            if (!preferred.isEmpty()) {
                Collections.shuffle(preferred); // 随机选择偏好位置
                String position = preferred.get(0);
                assignments.put(player, position);
                remainingPositions.remove(position);
            }
        }

        // 第二阶段：处理未分配选手
        for (String player : players) {
            if (!assignments.containsKey(player)) {
                Collections.shuffle(remainingPositions);
                String position = remainingPositions.remove(0);
                assignments.put(player, position);
            }
        }

        return assignments;
    }

    public static void main(String[] args) {
        Map<String, List<String>> prefs = new HashMap<String, List<String>>() {{
            put("老刁", Arrays.asList("打野", "上单", "中单"));
            put("席威", Arrays.asList("射手", "辅助"));
            put("老李", Arrays.asList("打野", "中单"));
            put("梁宇", Arrays.asList("辅助", "中单"));
            put("老杨", Arrays.asList("上单", "中单"));
        }};

        List<String> players = Arrays.asList("席威", "老刁", "老李", "梁宇","老杨");
        //路人位置
        String luren = "";
        Map<String, String> result = assignPositions(players, prefs,luren);

        result.forEach((k,v) -> System.out.printf("%s ➔ %s%n", k, v));
        // 获取所有可能的位置
        List<String> allPositions = new ArrayList<>(
                Arrays.asList("上单", "中单", "打野", "射手", "辅助"));

        // 通过集合运算找出未分配的位置
        allPositions.removeAll(result.values());
        System.out.println("💡 未分配位置: " + (allPositions.isEmpty() ? "无" : allPositions.get(0)));
    }

}
